package com.backmanage.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.backmanage.entity.LoginInfo;
import com.backmanage.entity.ReturnJSON;
import com.backmanage.entity.User;
import com.backmanage.service.serviceI.BaseServiceI;
import com.backmanage.utils.IpUtil;
import com.backmanage.utils.MD5Util;
import com.backmanage.utils.StringUtil;

/**
 * @author tangjiang 
 * @version 创建时间：2016年6月27日 上午9:40:54
 * 类说明
 */
@Controller
public class LoginController {
	
	private static Logger log = Logger.getLogger ( LoginController.class.getName ());	
	
	@Autowired
	private BaseServiceI baseService;
	@ResponseBody
	@RequestMapping("/test")
	public String test(HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException{
		String str = "11111112";
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(str);
		json = "m1(" + str + ")";
		json+=ssssss();
		return json;
	}
	public String ssssss(){
		return "33333333333";
	}
	/**
	 * 验证登录 添加登录信息
	 * @param user
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "login")
	public ReturnJSON Login(User user,HttpServletRequest request){
		
		ReturnJSON json = new ReturnJSON();
		
		Map map = new HashMap();
		
		/*用户名 密码不能为空*/
		if(StringUtil.isNotEmpty(user.getAccount())&&StringUtil.isNotEmpty(user.getPassword())){
			
			map.put("account", user.getAccount());
			
			map.put("password", MD5Util.toMD5(user.getPassword()));
			
		}else{
			
			log.info("账号密码不能为空");
			
			json.setMsg("账号密码不能为空");
			
			json.setCode(1);
			
			return json;
		}
		List<User> listUser = baseService.getByProperty(User.class, map);
		
		if(listUser!=null&&listUser.size()>0){
			LoginInfo info = new LoginInfo();
			info.setUser_id(listUser.get(0).getId());
			String ip=null;
			try {
				ip = IpUtil.getIpAddress(request);
				info.setIp(ip);
			} catch (IOException e) {
				log.error("获取IP错误: "+e);
				json.setMsg("获取IP错误");
				json.setCode(1);
				return json;
			}
			/*设置登录列表*/
			info.setMsg("已在IP为："+ip+"登录");
			LoginInfo.listLoginInfo.put("login"+listUser.get(0).getId(), info);
			
			/*写入到session*/
			request.getSession().setAttribute("ip", ip);
			request.getSession().setAttribute("id", listUser.get(0).getId());
		}
		/*返回数据*/
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("user", listUser.get(0));
		json.setData(jsonObject);
		
		return json;
	}
}
